package xcu.lxj.nettychat.websocket.netty;

import io.netty.channel.Channel;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

/**
 * @Author XinJian Lei
 * @Date 2024/10/2 10:46
 * @Version 1.0
 */
public class NettyAttrUtils {

    public static AttributeKey<String> TOKEN = AttributeKey.valueOf("token");
    public static AttributeKey<String> IP = AttributeKey.valueOf("ip");
    public static AttributeKey<Long> UID = AttributeKey.valueOf("uid");

    /**
     * 存储属性
     * @param channel
     * @param attributeKey AttributeKey<T>
     * @param data 存入的数据
     */
    public static <T> void setAttr(Channel channel, AttributeKey<T> attributeKey, T data) {
        Attribute<T> attr = channel.attr(attributeKey);
        attr.set(data);
    }

    /**
     * 获得对应的属性
     * @param channel
     * @param key
     * @return
     * @param <T>
     */
    public static <T> T getAttr(Channel channel, AttributeKey<T> key) {
        return channel.attr(key).get();
    }
}